package Demo1;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-07-14
 * Time: 9:33
 */
public class Test {
    public static boolean isValid(String s) {
        Stack<String> stack = new Stack<>();

        //遍历字符串，左括号放到栈里，轮到右括号拿出进行比较
        for(int i = 0;i < s.length();i++){
            String ret = s.charAt(i) + "";
            if(ret.equals("{") || ret.equals("(") || ret.equals("[")){
                stack.push(ret);
            }else{
                if(!stack.isEmpty()){
                    if((stack.peek().equals("{") && ret.equals("}")) || (stack.peek().equals("(") && ret.equals(")")) || (stack.peek().equals("[") && ret.equals("]"))){
                        stack.pop();
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
//    //1.使用递归逆序打印链表
//    public void func1(ListNode cur){
//        if(cur != null){
//            func(cur.next);
//            System.out.print(cur.val + " ");
//        }
//        return;
//    }

    //2.使用栈逆序打印链表
    public static void func2(List<Integer> c){
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < c.size(); i++) {
            stack.push(c.get(i));
        }
        while(!stack.empty()){
            System.out.print(stack.pop() + " ");
        }
    }
    public static void main(String[] args) {
        LinkedList<Integer> list1 = new LinkedList<>();
        list1.add(0,34);
        list1.add(1,33);
        list1.add(2,31);
        list1.add(3,30);
        func2(list1);
    }
    public static void main6(String[] args) {
        LinkedList<Integer> list1 = new LinkedList<>();
        list1.add(0,34);
        list1.add(1,33);
        list1.add(2,31);
        list1.add(3,30);
        //遍历链表的方法
        //1.直接打印
        System.out.println(list1);

        //2.for 循环打印
        for (int i = 0; i < list1.size(); i++) {
            System.out.print(list1.get(i) + " ");
        }
        System.out.println();

        //3.for - each 打印
        for(int x : list1){
            System.out.print(x + " ");
        }
        System.out.println();

        //4.迭代器 Iterator 打印
        Iterator<Integer> it =  list1.iterator();
        while(it.hasNext()){
            System.out.print(it.next() + " ");
        }
        System.out.println();

        //5.迭代器 ListIterator 打印
        ListIterator it2 = list1.listIterator();
        while(it2.hasNext()){
            System.out.print(it2.next() + " ");
        }
        System.out.println();

        //6.迭代器逆序打印
        ListIterator it3 = list1.listIterator(list1.size());
        while(it3.hasPrevious()){
            System.out.print(it3.previous() + " ");
        }
        System.out.println();
    }
    public static void main5(String[] args) {
        LinkedList<Integer> list1 = new LinkedList<>();
        list1.add(0,3);
        list1.add(1,3);
        list1.add(2,3);
        List<Integer> list2 = list1.subList(0,3);
        System.out.println(list1);
        System.out.println(list2);
        list2.set(0,999);
        list2.remove(new Integer(999));
        System.out.println(list1);
        System.out.println(list2);
    }
    public static void main4(String[] args) {
        LinkedList<Integer> list1 = new LinkedList<>();
        list1.add(0,3);
        list1.add(1,3);
        list1.add(2,3);
        System.out.println(list1);
//        list1.remove(new Integer(3));
//        list1.remove(1);
        list1.remove();//默认是头删
        System.out.println(list1);

    }
    public static void main3(String[] args) {
        LinkedList<Integer> list1 = new LinkedList<>();
        list1.add(0,1);
        list1.add(1,2);
        list1.add(2,3);
        System.out.println(list1);
        LinkedList<Integer> list2 = new LinkedList<>();
        list2.add(44);//默认是尾插
        list2.add(44);
        list2.add(44);
        list2.addAll(list1);
        System.out.println(list2);
    }
    public static void main2(String[] args) {
        LinkedList<Integer> list1 = new LinkedList<>();
        list1.add(1);//默认是尾插
        list1.add(2);
        list1.add(3);
        System.out.println(list1);
        LinkedList<Integer> list2 = new LinkedList<>(list1);
        System.out.println(list2);
    }
    public static void main1(String[] args) {
        boolean ret = isValid("()");
        System.out.println(ret);
    }
}
